import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hzy_project_template/base_ui_config/common_state_widget.dart';
import 'package:hzy_project_template/config/config_index.dart';

enum InputType {
  /// 输入框
  onlyTf,

  /// 标题 + 输入框
  titleAndTf,

  /// 标题 + 输入框 + 其他
  more,
}

enum InputLayoutType {
  rowStart,
  rowBetween,
  rowEnd,
}

class InputModel {
  /// =============== 界面样式 =============== ///
  InputType? type;

  /// =============== 界面布局 =============== ///

  InputLayoutType? inputLayoutType;

  /// =============== left =============== ///
  String? leftText;
  Widget? leftWidget;
  double? leftFontSize;
  Color? leftFontColor;
  TextStyle? leftStyle;
  double? leftWidth;
  EdgeInsetsGeometry? leftPadding;

  /// =============== 输入框

  String? hintText;
  String? text;
  bool? isShowLine = true;
  TextAlign? textAlign = TextAlign.left;
  TextEditingController? textEditingController;
  double? fontSize;
  TextInputType? inputType;
  int? maxLines = 1;
  TextStyle? hintStyle;
  TextStyle? style;
  EdgeInsetsGeometry? contentPadding;
  int? maxLengtg;
  bool? obscureText;
  bool? enabled;
  bool? isExpanded;
  Widget? suffixIcon;
  TextInputAction? textInputAction;
  List<TextInputFormatter>? inputFormatters;

  /// =============== right =============== ///
  Widget? rightWidgt;

  InputModel({
    this.hintText = "请输入",
    this.text = "",
    this.leftText,
    this.leftFontColor,
    this.leftFontSize,
    this.leftWidth,
    this.leftPadding = const EdgeInsets.only(right: 20),
    this.leftStyle,
    this.leftWidget,
    this.maxLines = 1,
    this.isShowLine = true,
    this.type = InputType.onlyTf,
    this.fontSize = 16,
    this.inputType,
    this.maxLengtg,
    this.obscureText = false,
    this.textAlign = TextAlign.left,
    this.enabled,
    this.textEditingController,
    this.rightWidgt,
    this.isExpanded = true,
    this.hintStyle,
    this.suffixIcon,
    this.inputLayoutType = InputLayoutType.rowStart,
    this.style,
    this.contentPadding,
    this.textInputAction = TextInputAction.go,
    this.inputFormatters,
  });
}

class NormalInputWidget extends StatefulWidget {
  final InputModel inputModel;
  const NormalInputWidget({
    super.key,
    required this.inputModel,
  });
  @override
  State<StatefulWidget> createState() {
    return NormalInputState();
  }
}

class NormalInputState extends CommonStateWidget<NormalInputWidget> {
  FocusNode? numberFocusNode;
  late InputModel inputModel;
  @override
  void initState() {
    inputModel = widget.inputModel;
    super.initState();
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Container();

    switch (inputModel.type) {
      case InputType.onlyTf:
        body = createOnlyTfWidget();
        break;
      case InputType.titleAndTf:
        body = createTitleAndTfWidget();
        break;
      case InputType.more:
        body = createMoreWidget();
        break;
      default:
    }
    return body;
  }

  /// 配置只有输入框
  createOnlyTfWidget() {
    inputModel.isExpanded = false;
    Widget body = configTextFiledWidget();
    return body;
  }

  /// 配置有 标题 + 输入框
  createTitleAndTfWidget() {
    List<Widget> children = const <Widget>[];
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start;
    switch (inputModel.inputLayoutType) {
      case InputLayoutType.rowStart:
        mainAxisAlignment = MainAxisAlignment.start;
        break;
      case InputLayoutType.rowBetween:
        mainAxisAlignment = MainAxisAlignment.spaceBetween;
        break;
      case InputLayoutType.rowEnd:
        mainAxisAlignment = MainAxisAlignment.end;
        break;
      default:
    }
    children = [
      configTitleWidget(),
      configTextFiledWidget(),
    ];
    Widget body = Row(
      mainAxisAlignment: mainAxisAlignment,
      children: children,
    );
    return body;
  }

  /// 配置多个视图
  createMoreWidget() {
    List<Widget> children = const <Widget>[];
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start;
    switch (inputModel.inputLayoutType) {
      case InputLayoutType.rowStart:
        mainAxisAlignment = MainAxisAlignment.start;
        break;
      case InputLayoutType.rowBetween:
        mainAxisAlignment = MainAxisAlignment.spaceBetween;
        break;
      case InputLayoutType.rowEnd:
        mainAxisAlignment = MainAxisAlignment.end;
        break;
      default:
    }
    children = [
      configTitleWidget(),
      configTextFiledWidget(),
      configRightWidget(),
    ];
    Widget body = Row(
      mainAxisAlignment: mainAxisAlignment,
      children: children,
    );
    return body;
  }

  /// 配置标题
  configTitleWidget() {
    Widget body = inputModel.leftText == null
        ? Container()
        : Container(
            padding: inputModel.leftPadding,
            width: inputModel.leftWidth,
            child: Text(
              inputModel.leftText!,
              style: configTitleStyle(type: 1),
            ),
          );
    body = inputModel.leftWidget ?? body;
    return body;
  }

  // 创建输入框
  configTextFiledWidget() {
    TextEditingController textEditingController =
        TextEditingController(text: inputModel.text);
    textEditingController.selection = TextSelection.fromPosition(
      TextPosition(
        offset: inputModel.text?.length ?? 0,
      ),
    );
    Widget body = createNormaltfWidget(
      context: context,
      textAlign: inputModel.textAlign!,
      textEditingController: textEditingController,
      hintText: inputModel.hintText,
      fontsize: inputModel.fontSize,
      maxLines: inputModel.maxLines,
      keyboardType: inputModel.inputType,
      maxLength: inputModel.maxLengtg,
      obscureText: inputModel.obscureText!,
      isExpanded: inputModel.isExpanded!,
      focusNode: numberFocusNode,
      enabled: inputModel.enabled,
      contentPadding: inputModel.contentPadding,
      textInputAction: inputModel.textInputAction,
      inputFormatters: inputModel.inputFormatters,
      prefixIcon: inputModel.suffixIcon,
      onChange: (value) {
        inputModel.text = value;
        if (inputModel.maxLengtg != null &&
            value.length == inputModel.maxLengtg) {
          HzyNormalTools.keydissMiss(context);
        }
      },
      style: inputModel.style ?? configTitleStyle(),
      hintStyle: inputModel.hintStyle ?? configHintStyle(),
    );
    return body;
  }

  /// 配置右边试图
  configRightWidget() {
    Widget body = const Icon(
      Icons.arrow_forward_ios,
      size: 12,
      color: Color(0xffb1b3b8),
    );
    body = inputModel.rightWidgt ?? body;
    return body;
  }

  /// 配置默认title Style
  configTitleStyle({
    int type = 0,
  }) {
    TextStyle textStyle = HzyTextStyle.fontMedium(
      size: inputModel.leftFontSize ?? 14.sp,
      color: inputModel.leftFontColor ?? HzyCommonColor().wbgblacktextcolor,
    );

    return textStyle;
  }

  // 创建默认hint Style
  configHintStyle() {
    TextStyle textStyle = HzyTextStyle.fontMedium(
      size: 14.sp,
      color: HzyCommonColor().colplacetextcolor,
    );
    return textStyle;
  }
}
